<?php


/**
 * Return an array with all departements
 * @global type $babDB
 * @return type 
 */
function unsDepartement_getAllDepartments() {

    global $babDB;


    $res = $babDB->db_query("select id, departement_name as name, departement_groupID as groupID, departement_directorID as directorID from " . UNSDEPARTEMENT_DEPARTEMENTS_TABLE_NAME);

    $departements = array();
    $i = 0;
    while ($arr = $babDB->db_fetch_array($res)) {
        $departements[$i] = $arr;
        $i++;
    }
    return $departements;
}

/**
 * Return a departement with his 'id'
 * @global type $babDB
 * @param type $id
 * @return Array Departement [id, name, groupID, directorID] 
 */
function unsDepartement_getDepartment($id) {
    global $babDB;


    $res = $babDB->db_query("select id, departement_name as name, departement_groupID as groupID, departement_directorID as directorID from " . UNSDEPARTEMENT_DEPARTEMENTS_TABLE_NAME . " where id = " . $id);

    $departement = $babDB->db_fetch_array($res);

    return $departement;
}

/**
 * Add a departement
 * @global type $babDB
 * @param String $name
 * @param int $directorID 
 */
function unsDepartement_addDepartment($name, $directorID) 
{
    global $babDB, $babBody;

    if (null != $name && null != $directorID) {

        $groupID = bab_createGroup($name, " ", $directorID, unsDepartement_getGroupIDByName('uns_Groups'));// on crée le groupe du departement
        
        $directorGroupID = bab_createGroup('Director', 'Director of '.$name, $directorID, $groupID);
         bab_attachUserToGroup($directorID, $directorGroupID);

        if (0 != $groupID) {// Si != 0 alors le groupe a été crée et ça retourne l'id du group
            $babDB->db_query("INSERT INTO " . UNSDEPARTEMENT_DEPARTEMENTS_TABLE_NAME . " (departement_name, departement_groupID, departement_directorID, departement_directorGroupID, departement_datetime, departement_author) VALUES ('" . $name . "', " . $groupID . ", " . $directorID . ", " . $directorGroupID . ", now(), " . $GLOBALS['BAB_SESS_USERID'] . ");");
        } else {
            $babBody->addError(bab_translate("A departement with this name all ready exist !", $GLOBALS['babAddonFolder']));
        }
    } else {
        $babBody->addError(bab_translate("All fields are required!", $GLOBALS['babAddonFolder']));
    }
}

function unsDepartement_editDepartment($id, $name, $directorID)
{
    
    global $babDB, $babBody;

    $departement = unsDepartement_getDepartment($id);
    $groupID = $departement['groupID'];
    bab_updateGroup($groupID, $name, $description);
    $babDB->db_query("update " . UNSDEPARTEMENT_DEPARTEMENTS_TABLE_NAME . " set departement_name ='" . $name . "', departement_directorID =" . $directorID ." where id = " . $id);
}


function unsDepartement_deleteDepartement($id) 
{
    global $babDB;
    
    $listFormation = unsDepartement_getFormationsForDepartement($id);
    $listFormationLength = count($listFormation);
    
    static $k = 0;
    while ($k < $listFormationLength) {
        unsDepartement_deleteFormation($listFormation[$k]['id']);
        $k++;
    }
    
    
    
    $idDept = $id;
    $departement = unsDepartement_getDepartment($idDept);
    bab_removeGroup($departement['groupID']);
    $babDB->db_query("delete from " . UNSDEPARTEMENT_DEPARTEMENTS_TABLE_NAME . " where id = " . $idDept . ";");
    
}


?>
